home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ASME's Mechanical Engine…ing Toolkit 1997 December
/
ASME's Mechanical Engineering Toolkit 1997 December.iso
/
ai
/
prlg195b.lzh
/
EXPERT.LZH
/
KNAPSACK.PRO
< prev
next >
Wrap
Text File
|
1987-05-26
|
3KB
|
79 lines
/*
CS 475 Computer Science Language Lab. -- PROLOG
By John Sun
*/
/*
Instruction for knapsack
*/
knapsacks :- cls, introduction,
get_data ( N_item, Total_volume, Total_weight ),
solution ( Total_volume, Total_weight, 1 ), fail.
knapsacks :- exitsys.
introduction :-
print ('This game is to find the best item(s) to put into a '),
print ('knapsack.' ), nl,
print ('First, I need to tell you something about this game. ' ),
print ('This game is to fill the\n ' ),
print ('knapsack with some items each ' ),
print ('of them has a specific volume and weight. \n'),
print ('Next, you have to specify the volume and weight you want.'),
print ('\nThen I will figure out which item can fit in it. ' ),
print ('\nFinally, don\'t forget to '),
print ('put a period after each input.' ), nl, nl, nl.
get_data ( N_item, Total_volume, Total_weight ) :-
get_number_item ( N_item ),
get_volume ( Total_volume ),
get_weight ( Total_weight ),
build_item ( N_item ).
get_number_item ( N_item ) :-
nl, tab ( 10 ),
print ( 'Enter the numbers of ITEM you have : ' ),
read ( N_item ), asserta(max_item(N_item)), nl.
get_volume ( Total_volume ) :-
tab ( 10 ),
print ( 'Please enter the VOLUME you want : '),
read ( Total_volume ), nl.
get_weight ( Total_weight ) :-
tab ( 10 ),
print ( 'Please enter the WEIGHT you want : '),
read ( Total_weight ), nl.
build_item ( 0 ).
build_item ( N_item ) :-
N_item > 0, print ( '\n\nINPUT FOR THE ITEM ', N_item ),
print ( '\nTHE VOLUME: ' ), read ( Volume ),
print ( 'THE WEIGHT: ' ), read ( Weight ),
assertz ( item ( N_item, Volume, Weight ) ),
Temp is N_item - 1,
build_item ( Temp ).
solution ( 0, 0, _ ).
solution ( Target_volume, Target_weight, Candidate ) :-
Target_volume > 0, Target_weight > 0, max_item(X), Candidate =< X,
item ( Candidate, Volume, Weight ),
print ( 'I am thinking!!\n' ),
Temp_1 is Target_volume - Volume,
Temp_2 is Target_weight - Weight,
Temp_3 is Candidate + 1,
solution ( Temp_1, Temp_2, Temp_3 ),
print ( '\n\nPicked item ', Candidate, ' with volume ', Volume,
' unit(s) and weight ', Weight, ' unit(s).' ), nl.
solution ( Target_volume, Target_weight, Candidate ) :-
Target_volume > 0, Target_weight > 0,
max_item(X), Candidate =< X,
Temp is Candidate + 1,
solution ( Target_volume, Target_weight, Temp ).
?- knapsacks.